package cn.edu.henu.team.fragments;

import static cn.bmob.v3.Bmob.getApplicationContext;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;

import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.adapter.FrugKindsAdapter;
import cn.edu.henu.team.adapter.MedicineAdapter;
import cn.edu.henu.team.base.BaseFragment;
import cn.edu.henu.team.bean.books;
import cn.edu.henu.team.bean.disasterkinds;
import cn.edu.henu.team.bean.drugList;

public class fragment2_drugList extends BaseFragment {
    private ArrayList<disasterkinds> mdlist = new ArrayList<>(); //牙病类
    private ArrayList<drugList> mdlisttool = new ArrayList<>(); //某一牙病类内的药品
    private ListView listView;
    private FrugKindsAdapter adapter;
    private Medicine_Fragment myFragment;
    private SmartRefreshLayout refreshSrl;

    /*选中的item的位数号码*/
    public static int mPosition=0;

    public fragment2_drugList() {}

    public static fragment2_drugList newInstance() {
        fragment2_drugList fragment = new fragment2_drugList();
        return fragment;
    }

    @Override
    protected void clickEvents() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //拿到当前位置
                mPosition = position;
                for (int i = 0; i < mdlist.size(); i++) {
                    mdlist.get(i).setSelect(false);
                }
                mdlist.get(position).setSelect(true);
                getMedicine(mdlist.get(mPosition).getKind());
                //及时刷新adapter
                adapter.notifyDataSetChanged();
                for (int i = 0; i < mdlist.size(); i++) {
                    myFragment = new Medicine_Fragment();
                    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.mfragment_container, myFragment);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Medicine_Fragment.TAG, mdlisttool);
                    myFragment.setArguments(bundle);
                    fragmentTransaction.commit();
                }
            }
        });
    }

    @Override
    protected int initLayout()
    {
        return R.layout.fragment_2_druglist;
    }

    @Override
    protected void initView() {
        listView = mRootView.findViewById(R.id.mlistview);
        refreshSrl=mRootView.findViewById(R.id.refresh_druglist_srl);

        refreshSrl.setRefreshHeader(new ClassicsHeader(getApplicationContext()));
        refreshSrl.setRefreshFooter(new ClassicsFooter(getApplicationContext()));


        refreshSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshSrl.finishRefresh(1000/*,false*/);
                initData();
            }
        });
        refreshSrl.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(1000/*,false*/);//传入false表示加载失败
            }
        });
    }

    @Override
    protected void initData() {
        mdlist.clear();
        BmobQuery<disasterkinds> bmobQuery = new BmobQuery("disasterkinds");
        bmobQuery.order("-createdAt");
        bmobQuery.findObjects(new FindListener<disasterkinds>() {
            @Override
            public void done(List<disasterkinds> list, BmobException e) {
                if (e == null) {
                    if (list != null) {
                        for (disasterkinds at : list) {
                            mdlist.add(at);
                            adapter = new FrugKindsAdapter(getContext(),mdlist);
                            listView.setAdapter(adapter);

                        }
                    }
                } else {
                    System.out.println("失败了");
                }
                mdlist.get(mPosition).setSelect(true);
                getMedicine(mdlist.get(mPosition).getKind());
            }
        });

    }
    protected  void getMedicine(String kind)
    {
        mdlisttool.clear();
        BmobQuery<drugList> bmobQuery = new BmobQuery("drugList");
        bmobQuery.order("-createdAt");
        bmobQuery.addWhereEqualTo("kind",kind);
        bmobQuery.findObjects(new FindListener<drugList>() {
            @Override
            public void done(List<drugList> list, BmobException e) {
                if (e == null) {
                    if (list != null) {
                        for (drugList at : list) {
                            mdlisttool.add(at);
                            //设置对应的Fragment
                            myFragment = new Medicine_Fragment();
                            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                            fragmentTransaction.replace(R.id.mfragment_container, myFragment);
                            //通过bundle传值给MyFragment
                            Bundle bundle = new Bundle();
                            bundle.putSerializable(Medicine_Fragment.TAG,mdlisttool);
                            myFragment.setArguments(bundle);
                            fragmentTransaction.commit();
                        }
                    }
                }
                else{

                }
            }
        });
    }


}